package com.buaa.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.Group;
import org.apache.solr.client.solrj.response.GroupCommand;
import org.apache.solr.client.solrj.response.GroupResponse;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.GroupParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.buaa.dao.InstitutionDao;
import com.buaa.service.InstitutionService;
import com.buaa.utils.JacksonUtil;
import com.buaa.utils.SolrUtil;

/**
 * Title: 机构Service实现 <br/>
 * Description: <br/>
 * Company: BUAA
 *
 * @author 伟宏
 * @date 2017年1月15日
 */
@Service
public class InstitutionServiceImpl implements InstitutionService {

	@Autowired
	private InstitutionDao institutionDao;

	@Override
	public String sumInfo(String loctype) {
		String sql = null;
		switch (loctype) {
		case "province":
			sql = "SELECT province,COUNT(*),128.04741,47.35659,code FROM institution GROUP BY province";
			break;
		case "city":
			sql = "SELECT city,COUNT(*),city.lng,city.lat,ins.code FROM institution ins,city WHERE ins.city = city.city_name GROUP BY city,city.lng,city.lat";
			break;
		case "xian":
			sql = "SELECT county,COUNT(*),county.lng,county.lat,ins.code FROM institution ins,county WHERE ins.county = county.county_name GROUP BY county,county.lng,county.lat";
			break;
		case "danwei":
			sql = "SELECT name,addr,lng,lat,code FROM institution ins2 GROUP BY name,lng,lat";
			break;
		}
		List<Object[]> list = institutionDao.common2(sql);
		String res = "";
		Object[] objects = null;
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				objects = list.get(i);
				if (i == list.size() - 1) {
					res += objects[2] + "," + objects[3] + "," + objects[1] + "," + objects[0] + "," + loctype + "," + objects[4];
				} else {
					res += objects[2] + "," + objects[3] + "," + objects[1] + "," + objects[0] + "," + loctype + "," + objects[4] + "|";
				}
			}
		}
		return res;
	}

	@Override
	public Map<String, Long> getCount(String quyu, String loctype, String zhibiao) {
		String sql = "";
		if (zhibiao != null && zhibiao.equals("type")) {
			sql = "SELECT COUNT(*),inst.name FROM institution ins,institution_type inst WHERE ins.type = inst.code ";
			if (loctype.equals("city"))
				sql += " and city='" + quyu + "'";
			else if (loctype.equals("xian"))
				sql += " and county='" + quyu + "'";
			else if (loctype.equals("danwei"))
				sql += " and ins.name='" + quyu + "'";
			sql += " GROUP BY inst.name ORDER BY COUNT(*) DESC ";
		} else {
			sql = "select count(*),institution." + zhibiao + " from institution ";
			if (loctype.equals("city"))
				sql += " where city='" + quyu + "'";
			else if (loctype.equals("xian"))
				sql += " where county='" + quyu + "'";
			else if (loctype.equals("danwei"))
				sql += " where name='" + quyu + "'";
			sql += " group by institution." + zhibiao + " order by count(*) desc ";
		}

		Map<String, Long> metaData = new LinkedHashMap<String, Long>();
		List<Object[]> list = institutionDao.common2(sql);
		if (list == null)
			return metaData;
		for (Object[] objects : list) {
			metaData.put(objects[1].toString(), Math.round(Double.parseDouble(objects[0].toString())));
		}
		return metaData;
	}

	@Override
	public List<Object> findAllType() {
		return institutionDao.findAllType();
	}

	@Override
	public List<Object> findAllKind() {
		return institutionDao.findAllKind();
	}

	@Override
	public List<Object> findAllTech() {
		return institutionDao.findAllTech();
	}

	private SolrQuery processQuery(SolrQuery query, String dwmc, String dwlx, String dwmccxfs, String dwjm, String dwjsly) {
		if (StringUtils.isNotBlank(dwmc)) {
			switch (dwmccxfs) {
			case "1": // 全匹配
				query.setQuery("institution_name_s:" + dwmc);
				break;
			case "2": // 模糊匹配
				query.setQuery("institution_name:" + dwmc);
				break;
			case "3": // 局部全匹配
				query.setQuery("institution_name_s:*" + dwmc + "*");
				break;
			default:
				query.setQuery("*:*");
				break;
			}
		} else {
			query.setQuery("*:*");
		}
		if (StringUtils.isNotBlank(dwlx))
			query.addFilterQuery("institution_type:" + dwlx);
		if (StringUtils.isNotBlank(dwjm))
			query.addFilterQuery("institution_kind:" + dwjm);
		if (StringUtils.isNotBlank(dwjsly))
			query.addFilterQuery("institution_tech:" + dwjsly);
		return query;
	}

	public String contentSearch2(String loctype, String dwmc, String dwlx, String dwmccxfs, String dwjm, String dwjsly) {
		SolrClient solrClient = SolrUtil.getHttpSolrClient("institution");
		SolrQuery query = new SolrQuery();
		QueryResponse response = null;
		String res = "";

		query = processQuery(query, dwmc, dwlx, dwmccxfs, dwjm, dwjsly);
		// 设置分页信息
		query.setStart(0);
		query.setRows(Integer.MAX_VALUE);
		if (StringUtils.equals(loctype, "province")) {
			// 设置显示的Field的域集合
			query.setFields("institution_code");
			// 执行查询并返回结果
			System.out.println("contentSearch:province:\t" + query);
			try {
				response = solrClient.query(query);
				solrClient.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			long count = 0L;
			// 获取匹配结果总数
			if (response != null) {
				count = response.getResults().getNumFound();
			}
			return "黑龙江省," + count + ",127.91077,48.734684,province";
		} else if (StringUtils.equals(loctype, "danwei")) {
			// 设置显示的Field的域集合
			query.setFields("institution_addr,institution_name,institution_lng,institution_lat,institution_code");
			// 执行查询并返回结果
			System.out.println("contentSearch:danwei:\t" + query);
			try {
				response = solrClient.query(query);
				solrClient.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			long count = 0L;
			// 获取匹配结果总数
			if (response != null) {
				count = response.getResults().getNumFound();
				System.out.println("匹配结果总数：" + count);
				SolrDocumentList list = response.getResults();
				if (list != null) {
					for (SolrDocument doc : list) {
						res += doc.get("institution_name") + "," + doc.get("institution_addr") + "," + doc.get("institution_name") + "," + doc.get("institution_addr") + "," + loctype + "|";
					}
				}
			}
			return res;
		} else {
			String lng = null;
			String lat = null;
			String groupIndex = null;
			switch (loctype) {
			case "city":
				lng = "city_lng";
				lat = "city_lat";
				groupIndex = "institution_city";
				break;
			case "xian":
				lng = "county_lng";
				lat = "county_lat";
				groupIndex = "institution_county";
				break;
			default:
				return res;
			}
			// 设置显示的Field的域集合
			query.setFields(lng + "," + lat);
			// 设置分类信息 （默认每个group显示一条）
			query.setParam(GroupParams.GROUP, true);
			query.setParam(GroupParams.GROUP_FIELD, groupIndex);
			// 执行查询并返回结果
			System.out.println("contentSearch:" + loctype + ":\t" + query);
			try {
				response = solrClient.query(query);
				solrClient.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (response != null) {
				GroupResponse groupResponse = response.getGroupResponse();
				if (groupResponse != null) {
					List<GroupCommand> groupList = groupResponse.getValues();
					for (GroupCommand groupCommand : groupList) {
						List<Group> groups = groupCommand.getValues();
						for (Group group : groups) {
							System.out.println("contentSearch:类别总数：" + group.getResult().getNumFound());
							SolrDocumentList list = group.getResult();
							res += group.getGroupValue() + "," + group.getResult().getNumFound() + "," + list.get(0).get(lng) + "," + list.get(0).get(lat) + "," + loctype+ "," + "a" + "|";
						}
					}
				}
			}
			return res;
		}
	}

	private String processSql(String sql, String dwmc, String dwlx, String dwmccxfs, String dwjm, String dwjsly) {
		if (!dwmc.equals(""))
			if (dwmccxfs.equals("1"))
				sql += " and ins.name LIKE '" + dwmc + "'";
			else
				sql += " and ins.name LIKE '%" + dwmc + "%'";
		if (!dwlx.equals(""))
			sql += " and inst.name = '" + dwlx + "'";
		if (!dwjm.equals(""))
			sql += " and ins.kind = '" + dwjm + "'";
		if (!dwjsly.equals(""))
			sql += " and ins.tech_parent = '" + dwjsly + "'";
		return sql;
	}

	@Override
	public String contentSearch(String loctype, String dwmc, String dwlx, String dwmccxfs, String dwjm, String dwjsly) {
		String res = "";
		String sql = null;
		switch (loctype) {
		case "province":
			sql = "SELECT COUNT(*),'黑龙江省',127.91077,48.734684 FROM institution ins ,institution_type inst WHERE ins.type = inst.code ";
			sql = processSql(sql, dwmc, dwlx, dwmccxfs, dwjm, dwjsly);
			break;
		case "city":
			sql = "SELECT COUNT(*),ins.city,city.lng,city.lat FROM institution ins,city,institution_type inst WHERE ins.city = city_name and ins.type = inst.code ";
			sql = processSql(sql, dwmc, dwlx, dwmccxfs, dwjm, dwjsly);
			sql += " GROUP BY ins.city,city.lng,city.lat";
			break;
		case "xian":
			sql = "SELECT COUNT(*),ins.county,county.lng,county.lat FROM institution ins,county,institution_type inst WHERE ins.county = county_name and ins.type = inst.code ";
			sql = processSql(sql, dwmc, dwlx, dwmccxfs, dwjm, dwjsly);
			sql += " GROUP BY ins.county,county.lng,county.lat";
			break;
		case "danwei":
			sql = "SELECT ins.addr,ins.name,ins.lng,ins.lat FROM institution ins,institution_type inst WHERE ins.type = inst.code ";
			sql = processSql(sql, dwmc, dwlx, dwmccxfs, dwjm, dwjsly);
			// sql += " GROUP BY ins.name,ins.lng,ins.lat";
			break;
		}
		System.out.println(sql);

		List<Object[]> list = institutionDao.common2(sql);
		if (list == null)
			return res;
		Object[] objects = null;
		for (int i = 0; i < list.size(); i++) {
			objects = list.get(i);
			if (i != list.size() - 1) {
				res += objects[1] + "," + objects[0] + "," + objects[2] + "," + objects[3] + "," + loctype + "|";
			} else {
				res += objects[1] + "," + objects[0] + "," + objects[2] + "," + objects[3] + "," + loctype;
			}
		}
		return res;
	}

	@Override
	public List<Object[]> getAllList(String loctype) {
		String sql = null;
		if (loctype.equals("city")) {
			sql = "select city,count(*) from institution group by city order by count(*) desc ";
		}
		return institutionDao.common2(sql);
	}

	@Override
	public List<Object> getCountList(String quyu, String loctype, String zhibiao) {
		String sql = "";
		if (zhibiao != null && zhibiao.equals("type")) {
			sql = "SELECT COUNT(*),inst.name FROM institution ins,institution_type inst WHERE ins.type = inst.code ";
			if (loctype.equals("city"))
				sql += " and city='" + quyu + "'";
			else if (loctype.equals("xian"))
				sql += " and county='" + quyu + "'";
			else if (loctype.equals("danwei"))
				sql += " and ins.name='" + quyu + "'";
			sql += " GROUP BY inst.name ORDER BY COUNT(*) DESC ";
		} else {
			sql = "select count(*)," + zhibiao + " from institution ";
			if (loctype.equals("city"))
				sql += " where city='" + quyu + "'";
			else if (loctype.equals("xian"))
				sql += " where county='" + quyu + "'";
			else if (loctype.equals("danwei"))
				sql += " where name='" + quyu + "'";
			sql += " group by " + zhibiao + " order by count(*) desc ";
		}
		System.out.println(sql);
		List<Object[]> countlist = institutionDao.common2(sql);
		String name;
		if (zhibiao.equals("type")) {
			name = "类型";
		} else if (zhibiao.equals("home")) {
			name = "产地";
		} else {
			name = "待定";
		}
		List<Object> ret = new ArrayList<>();
		ret.add(name);
		ret.add(countlist);
		return ret;
	}

	@Override
	public List<Object[]> getDetail(String quyu) {
		StringBuffer sb = new StringBuffer();
		sb.append("ins.name,");
		sb.append("city,");
		sb.append("inst.name type_name,");
		sb.append("depart,");
		sb.append("addr,");
		sb.append("ins.birth,");
		sb.append("ins.contact_name,");
		sb.append("ins.contact_job,");
		sb.append("ins.contact_phone,");
		sb.append("ins.county");
		String sql = "select " + sb.toString() + " from institution ins,institution_type inst where ins.type=inst.code and ins.name  ='" + quyu + "'";
		return institutionDao.common2(sql);
	}

	@Override
	public List<Object[]> getCxTable1Info(String dwlx, String dwmc, String dwmccxfs, String dwjm, String dwjsly, String quyu, String loctype) {
		String sql = "select ins.name,addr,ins.code  from institution ins,institution_type inst where ins.type=inst.code ";
		if (loctype.equals("city"))
			sql += " and city = '" + quyu + "'";
		else if (loctype.equals("xian"))
			sql += " and county  = '" + quyu + "'";
		else if (loctype.equals("danwei"))
			sql += " and ins.name  = '" + quyu + "'";
		if (!dwmc.equals(""))
			if (dwmccxfs.equals("1"))
				sql += " and ins.name like '" + dwmc + "'";
			else
				sql += " and ins.name like '%" + dwmc + "%'";
		if (dwlx != null && !dwlx.equals(""))
			sql += " and  inst.name = '" + dwlx + "'";
		if (dwjm != null && !dwjm.equals(""))
			sql += " and  ins.kind = '" + dwjm + "'";
		if (dwjsly != null && !dwjsly.equals(""))
			sql += " and  ins.tech_parent = '" + dwjsly + "'";
		System.out.println(sql);
		return institutionDao.common2(sql);
	}

	@Override
	public List<Object[]> getCxTable1Info2(String dwlx, String dwmc, String dwmccxfs, String dwjm, String dwjsly, String quyu, String loctype) {
		SolrClient solrClient = SolrUtil.getHttpSolrClient("institution");
		SolrQuery query = new SolrQuery();
		// 输入查询条件
		if (StringUtils.equals(loctype, "danwei") && StringUtils.isNotBlank(quyu)) {
			query.setQuery("institution_name_s:" + dwmc);
		} else if (StringUtils.isNotBlank(dwmc)) {
			switch (dwmccxfs) {
			case "1": // 全匹配
				query.setQuery("institution_name_s:" + dwmc);
				break;
			case "2": // 模糊匹配
				query.setQuery("institution_name:" + dwmc);
				break;
			case "3": // 局部全匹配
				query.setQuery("institution_name_s:*" + dwmc + "*");
				break;
			default:
				query.setQuery("*:*");
				break;
			}
		} else {
			query.setQuery("*:*");
		}
		// 输入过滤条件
		switch (loctype) {
		case "city":
			query.addFilterQuery("institution_city:" + quyu);
			break;
		case "xian":
			query.addFilterQuery("institution_county:" + quyu);
			break;
		case "danwei":
			// 前面已处理
			break;
		}
		if (StringUtils.isNotBlank(dwlx))
			query.addFilterQuery("institution_name:" + dwlx);
		if (StringUtils.isNotBlank(dwjm))
			query.addFilterQuery("institution_kind:" + dwjm);
		if (StringUtils.isNotBlank(dwjsly))
			query.addFilterQuery("institution_tech:" + dwjsly);
		// 设置分页信息（使用默认的）
		query.setStart(0);
		query.setRows(Integer.MAX_VALUE);
		// 执行查询并返回结果
		System.out.println("getCxTable1Info:" + query);
		QueryResponse response = null;
		try {
			response = solrClient.query(query);
			solrClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 获取匹配的所有结果
		List<Object[]> ret = new ArrayList<>();
		Object[] objects = null;
		SolrDocumentList list = response.getResults();
		// 匹配结果总数
		long count = list.getNumFound();
		System.out.println("匹配结果总数:" + count);
		for (SolrDocument doc : list) {
			objects = new Object[3];
			objects[0] = doc.get("institution_name");
			objects[1] = doc.get("institution_addr");
			objects[2] = doc.get("institution_code");
			ret.add(objects);
		}
		return ret;
	}

	@Override
	public List<Object> getTable1Info(String zhibiao, String zhibiaomc, String quyu, String loctype, String page) {
		String sql = "";
		String sqlnum = "";
		if (zhibiao != null && zhibiao.equals("type")) {
			sql = "SELECT ins.name,addr,ins.code FROM institution ins,institution_type inst WHERE ins.type = inst.code ";
			sqlnum = "SELECT COUNT(*) FROM institution ins,institution_type inst WHERE ins.type = inst.code";
			if (loctype.equals("province")) {
				// 不用操作
			} else if (loctype.equals("city")) {
				sql += " AND city ='" + quyu + "' ";
				sqlnum += " AND city ='" + quyu + "' ";
			} else if (loctype.equals("xian")) {
				sql += " AND county ='" + quyu + "' ";
				sqlnum += " AND county ='" + quyu + "' ";
			}
			sql += " and inst.name='" + zhibiaomc + "'";
			sqlnum += " and inst.name='" + zhibiaomc + "'";
		} else {
			sql = "SELECT name,addr,code FROM institution WHERE 1=1 ";
			sqlnum = "SELECT COUNT(*) FROM institution WHERE 1=1";
			if (loctype.equals("province")) {
				// 不用操作
			} else if (loctype.equals("city")) {
				sql += " AND city ='" + quyu + "' ";
				sqlnum += " AND city ='" + quyu + "' ";
			} else if (loctype.equals("xian")) {
				sql += " AND county ='" + quyu + "' ";
				sqlnum += " AND county ='" + quyu + "' ";
			}
			if (!zhibiao.equals("")) {
				sql += " and " + zhibiao + "='" + zhibiaomc + "'";
				sqlnum += " and " + zhibiao + "='" + zhibiaomc + "'";
			}
		}
		System.out.println(sql);

		int curpage = 1;
		if (page != null)
			curpage = Integer.valueOf(page);
		// 总页数
		int totalpage = 0;
		// 下一页
		int nextpage = 0;
		// 上一页
		int lastpage = 0;
		// 每页显示条数
		int pagenum = 10;
		// 数据总条数
		int total = 0;
		List<Object> rsnum = institutionDao.common1(sqlnum);
		if (rsnum != null) {
			if (Integer.valueOf(rsnum.get(0).toString()) % pagenum != 0)
				totalpage = Integer.valueOf(rsnum.get(0).toString()) / pagenum + 1;
			else
				totalpage = Integer.valueOf(rsnum.get(0).toString()) / pagenum;
			total = Integer.valueOf(rsnum.get(0).toString());
		}
		if (curpage == 1)
			lastpage = 1;
		else
			lastpage = curpage - 1;
		if (curpage < totalpage)
			nextpage = curpage + 1;
		else {
			nextpage = totalpage;
			pagenum = total - pagenum * (totalpage - 1);
		}
		// 显示区间
		int beginnum = (curpage - 1) * 10;
		int endnum = beginnum + pagenum;

		List<Object[]> rs = institutionDao.common2(sql);
		List<Object> ret = new ArrayList<>();
		ret.add(totalpage);
		ret.add(total);
		ret.add(curpage);
		ret.add(nextpage);
		ret.add(lastpage);
		ret.add(pagenum);
		ret.add(beginnum);
		ret.add(endnum);
		ret.add(rs);
		return ret;
	}

	@Override
	public List<Object[]> getTable2Info(String id) {
		StringBuffer sb = new StringBuffer();
		sb.append("ins.name,");
		sb.append("city,");
		sb.append("inst.name type_name,");
		sb.append("depart,");
		sb.append("addr,");
		sb.append("ins.birth,");
		sb.append("ins.contact_name,");
		sb.append("ins.contact_job,");
		sb.append("ins.contact_phone,");
		sb.append("ins.county");
		String sql = "select " + sb.toString() + " from institution ins,institution_type inst where ins.type=inst.code and ins.code ='" + id + "'";
		return institutionDao.common2(sql);
	}


	@Override
	public Map<String, Object> getInstitutionByResource(String id,String resource) {
		StringBuffer sb = new StringBuffer();
		sb.append("ins2.code,");
		sb.append("ins2.name,");
		sb.append("ins2.city,");
		sb.append("inst.name type_name,");
		sb.append("ins2.depart,");
		sb.append("ins2.addr,");
		sb.append("ins2.birth,");
		sb.append("ins2.contact_name,");
		sb.append("ins2.contact_job,");
		sb.append("ins2.contact_phone,");
		sb.append("ins2.county");
		String sql = null;
		switch (resource) {
		case "instrument":
			sql = "select " + sb.toString() + " FROM instrument ins1,institution ins2,institution_type inst WHERE ins1.id = " + id + " AND ins1.unit_code = ins2.code AND ins2.type = inst.code";
			break;
		case "project":
			sql = "select " + sb.toString() + " FROM project ins1,institution ins2,institution_type inst WHERE ins1.id = " + id + " AND ins1.unit_code = ins2.code AND ins2.type = inst.code";
			break;
		case "talent":
			sql = "select " + sb.toString() + " FROM talent ins1,institution ins2,institution_type inst WHERE ins1.id = " + id + " AND ins1.unit_code = ins2.code AND ins2.type = inst.code";
			break;
		case "base":
			sql = "select " + sb.toString() + " FROM base ins1,institution ins2,institution_type inst WHERE ins1.id = " + id + " AND ins1.unit_code = ins2.code AND ins2.type = inst.code";
			break;
		default:
			break;
		}	
		List<Object[]> ret = institutionDao.common2(sql);
		Map<String, Object> map = new LinkedHashMap<>();
		if (CollectionUtils.isNotEmpty(ret)) {
			Object[] objects = ret.get(0);
			map.put("机构编码", objects[0]);
			map.put("机构名称", objects[1]);
			map.put("所在城市", objects[2]);
			map.put("机构类型", objects[3]);
			map.put("上级主管部门", objects[4]);
			map.put("机构地址", objects[5]);
			map.put("成立时间", objects[6]);
			map.put("联系人姓名", objects[7]);
			map.put("联系人职务", objects[8]);
			map.put("联系人电话", objects[9]);
			map.put("所在区县", objects[10]);
		}
		return map;
	}

	@Override
	public Map<String, Object> getInstitutionDetail(String id) {
		StringBuffer sb = new StringBuffer();
		sb.append("ins.code,");
		sb.append("ins.name,");
		sb.append("ins.kind,");
		sb.append("ins.city,");
		sb.append("ins.county,");
		sb.append("ins.street,");
		sb.append("inst.name type_name,");
		sb.append("ins.depart,");
		sb.append("ins.addr,");
		sb.append("ins.birth,");
		sb.append("ins.contact_name,");
		sb.append("ins.contact_job,");
		sb.append("ins.contact_phone,");
		sb.append("ins.cor_name,");
		sb.append("ins.cor_job,");
		sb.append("ins.reg_money,");
		sb.append("ins.tech_parent,");
		sb.append("ins.tech_child,");
		sb.append("ins.url");
		String sql = "select " + sb.toString() + " from institution ins,institution_type inst where ins.type=inst.code and ins.code ='" + id + "'";
		List<Object[]> ret = institutionDao.common2(sql);
		Map<String, Object> map = new LinkedHashMap<>();
		if (CollectionUtils.isNotEmpty(ret)) {
			Object[] objects = ret.get(0);
			map.put("机构编码", objects[0]);
			map.put("机构名称", objects[1]);
			map.put("机构种类", objects[2]);
			map.put("所在城市", objects[3]);
			map.put("所在区县", objects[4]);
			map.put("所在街道", objects[5]);
			map.put("机构类型", objects[6]);
			map.put("上级主管单位", objects[7]);
			map.put("机构地址", objects[8]);
			map.put("成立时间", objects[9]);
			map.put("联系人姓名", objects[10]);
			map.put("联系人职务", objects[11]);
			map.put("联系人电话", objects[12]);
			map.put("法人姓名", objects[13]);
			map.put("法人职务", objects[14]);
			map.put("注册资金", objects[15]);
			map.put("技术领域（父）", objects[16]);
			map.put("技术领域（子）", objects[17]);
			map.put("机构网址", objects[18]);
		}
		return map;
	}

	@Override
	public String[][] getCp(String totalid) {
		// 哈尔滨市@city;双鸭山市@city;伊春市@city
		String[] id = totalid.split(";");
		String[][] data = new String[id.length][3];
		List<Object> query = null;

		for (int i = 0; i < id.length; i++) {
			if (StringUtils.isNotBlank(id[i])) {
				String[] quyu = id[i].split("@");
				String sql = "select count(*) from institution where kind='普通机构' and  ";
				String sql1 = "select count(*) from institution where kind='加盟机构' and  ";
				if (quyu[1].equals("city")) {
					sql += " city ='" + quyu[0] + "'";
					sql1 += " city ='" + quyu[0] + "'";
				}
				if (quyu[1].equals("xian")) {
					sql += " county ='" + quyu[0] + "'";
					sql1 += " county ='" + quyu[0] + "'";
				}
				data[i][0] = quyu[0];
				query = institutionDao.common1(sql);
				if (query != null && query.size() != 0) {
					data[i][1] = query.get(0).toString();
				}
				query = institutionDao.common1(sql1);
				if (query != null && query.size() != 0) {
					data[i][2] = query.get(0).toString();
				}
			}
		}
		return data;
	}

	@Override
	public String getSum(String zhibiao) {
		Map<String, Long> data = getCount("", "", zhibiao);
		Set<Map<String, Object>> set = new LinkedHashSet<>();
		Map<String, Object> map;
		for (Map.Entry<String, Long> entry : data.entrySet()) {
			map = new HashMap<>();
			map.put("name", entry.getKey());
			map.put("value", entry.getValue());
			set.add(map);
		}
		return JacksonUtil.toJSon(set);
	}

	@Override
	public List<Object[]> getFb() {
		String sql = "SELECT ins.lng,ins.lat,1,code FROM institution ins ";
		return institutionDao.common2(sql);
	}

	@Override
	public List<Map<String, Object>> getFb2() {
		String sql = "SELECT ins.city,COUNT(*) FROM institution ins,city WHERE ins.city = city.city_name GROUP BY ins.city ";
		List<Object[]> list = institutionDao.common2(sql);

		List<Map<String, Object>> ret = new ArrayList<>();
		Map<String, Object> map = null;
		for (Object[] objects : list) {
			map = new HashMap<>();
			map.put("name", objects[0]);
			map.put("value", objects[1]);
			ret.add(map);
		}
		return ret;
	}

	@Override
	public String getCitySum() {
		String sql = "select city,count(*) from Institution group by city";
		List<Object[]> list = institutionDao.common2(sql);
		Set<Map<String, Object>> set = new TreeSet<Map<String, Object>>(new Comparator<Map<String, Object>>() {
			@Override
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				try {
					int i = Integer.valueOf(o1.get("value").toString()).compareTo(Integer.valueOf(o2.get("value").toString()));
					return i > 0 ? 1 : i < 0 ? -1 : 1;
				} catch (Exception e) {
					e.printStackTrace();
					return 1;
				}
			}
		});
		Map<String, Object> map;
		Set<String> citySet = new HashSet<String>();
		citySet.add("哈尔滨市");
		citySet.add("牡丹江市");
		citySet.add("鸡西市");
		citySet.add("佳木斯市");
		citySet.add("齐齐哈尔市");
		citySet.add("大庆市");
		citySet.add("鹤岗市");
		citySet.add("绥化市");
		citySet.add("黑河市");
		citySet.add("七台河市");
		citySet.add("伊春市");
		citySet.add("大兴安岭地区");
		citySet.add("双鸭山市");
		for (Object[] objects : list) {
			String name = objects[0].toString();
			// System.out.println(name);
			// 数据表中有填"其他市"，有没有填的
			if (name.equals("其他市") || name.equals(""))
				continue;
			map = new HashMap<String, Object>();
			Integer value = 0;
			try {
				value = Integer.valueOf(objects[1].toString());
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (citySet.contains(name)) {
				map.put("name", name);
				map.put("value", value);
				set.add(map);
			}
		}
		return JacksonUtil.toJSon(set);
	}

}
